﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kinect.Toolbox
{
    class Playlist
    {
        const int NUM_of_SONGS = 10;
        const int NUM_of_COLUMNS = 20;

        int[ , ] songList;
        /* SongList = Num_of_Songs * Num_of_COLUMNS
         * e.g)
         * |SongNumber| Learned Emtions ------------------------ | Sum of L.Emotion | pos of Next Write | SongType |
         * |     1    | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | ........ |         6        |          9        |   Happy  |
         * |     2    | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | ........ |         1        |          9        |  Neutral |
         * |    ...
         */
                
        static ReadFile read = new ReadFile();
        int learnedEmotion = read.getLearnedEmotion();

        int sum = NUM_of_COLUMNS - 3;
        static int position = NUM_of_COLUMNS - 2;
        int songType = NUM_of_COLUMNS - 1;
        int NofEmotions = position - 1;

        /// XXX      
        double RATIO = 0.35; // general emotion - more than 35% of leanred emotions

       
        public void initList()
        {
            songList = new int[NUM_of_SONGS, NUM_of_COLUMNS];
            for (int i = 0; i < NUM_of_SONGS; i++)
            {
                for (int j = 1; j < NUM_of_COLUMNS; j++)
                    songList[i, j] = 0;
                songList[i, 0] = i + 1;
                songList[i, NUM_of_COLUMNS - 1] = 1;    //setting next write position to [i,1]
            }

            /*
             songList = new int[NUM_of_SONGS, NUM_of_EMOTIONS]  { 	
             { 1, }, 
             { 2, }, 
             { 3, }, 
             { 4, }, 
             { 5, }, 
             { 6, }, 
             { 7, }, 
             { 8, }, 
             { 9, },              
             { 10, } 
             };
             */
        }

        // Called when song changes (<<, >> or song ended) and need to update learned emotion
        // int n: currect song number (or past depending on how we decide to program)
        public void updateSongList(int n)
        {   
            int next = songList[n, position]; // write position
            songList[n, next] = learnedEmotion; // put learned emotion into playlist
            songList[n, sum] += learnedEmotion; // update Sum

            // update SongType Happy:1, Neutral:0, Sad:-1
            if (sum / NofEmotions > RATIO)  
                songList[n, songType] = 1;
            else if (sum / NofEmotions < -RATIO) 
                songList[n, songType] = -1;
            else                          
                songList[n, songType] = 0;

            songList[n, position]++; // update next write position
        }

        public void updateLists()
        {
            ///XXX
            // Would it be better if we have lists to keep track of which song to play in which emotion?
            // Like, HappyList, SadList, NeutralList with songNumber of corresponding songs. 
        }

    }
}
